<?php
/**
 * Created by PhpStorm.
 * Script Name: MiniSetting.php
 * Create: 2023/2/7 11:59
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;


use app\common\service\App as AppService;

class MiniSetting extends Mini
{
    /**
     * 获取应用层面的配置
     * @param $app_id
     * @param string $column
     * @param string $name
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function getAppConfig($app_id, $column = '', $name= ''){
        $app = AppService::getAppInfo($app_id);
        if(empty($app['config'])){
            return [];
        }
        $config = json_decode($app['config'], true);
        if(!empty($config['extjson'])){
            $ext_json_arr = preg_split('/[\r\n]+/', trim($config['extjson'], "\r\n"));
            $ext_json = [];
            foreach ($ext_json_arr as $item){
                list($k, $v) = explode('|', $item);
                $ext_json[$k] = $v;
            }
            $config['extjson'] = $ext_json;
        }
        if(!empty($config['privacy']['setting_list'])) {
            $setting_arr = preg_split('/[\r\n]+/', trim($config['privacy']['setting_list'], "\r\n"));
            $setting_list = [];
            foreach ($setting_arr as $item) {
                list($k, $v) = explode('|', $item);
                $setting_list[$k] = $v;
            }
            $config['privacy']['setting_list'] = $setting_list;
        }
        if($column){
            $config = empty($config[$column]) ? [] : $config[$column];
        }
        if($name){
            $config = empty($config[$name]) ? [] : $config[$name];
        }
        return $config;
    }
}